package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"time"
	"tizhi_front/nspel"
)

type PrescriptionItem struct {
	Id             int64     `gorm:"primary_key;column:id"`
	PrescriptionId int64     `gorm:"column:prescription_id"`
	Order          int64     `gorm:"column:order"`
	Name           string    `gorm:"column:name"`
	Title          string    `gorm:"column:title"`
	PracticeNum    int64     `gorm:"column:practice_num"`
	PracticeTime   int64     `gorm:"column:practice_time"`
	IntervalTime   int64     `gorm:"column:interval_time"`
	Content        string    `gorm:"column:content"`
	CreateAt       time.Time `gorm:"column:create_at"`
	UpdateAt       time.Time `gorm:"column:update_at"`
}

func (p *PrescriptionItem) TableName() string {
	return "prescription_item"
}

type PrescriptionItemE struct{}

func (*PrescriptionItemE) SelectByPrescriptionIdOrderByOrderAsc(prescriptionId int64) (prescriptions []PrescriptionItem0, err error) {
	key := "prescription_item0.prescription_id:" + strconv.FormatInt(prescriptionId, 10) + ".order.asc"
	err = nspel.RedisGetGob(key, &prescriptions)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select([]string{"name", "title", "practice_num", "practice_time", "interval_time", "content"}).Where("prescription_id = ?", prescriptionId).Order("order").Find(&prescriptions).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, prescriptions)
	return
}

type PrescriptionItem0 struct {
	Name         string `gorm:"column:name"`
	Title        string `gorm:"column:title"`
	PracticeNum  int64  `gorm:"column:practice_num"`
	PracticeTime int64  `gorm:"column:practice_time"`
	IntervalTime int64  `gorm:"column:interval_time"`
	Content      string `gorm:"column:content"`
}

func (p *PrescriptionItem0) TableName() string {
	return "prescription_item"
}
